import 'dart:io';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/leave/leave_record_view_model.dart';

import 'finish_leave_page.dart';

class LeaveRecordPage extends BaseStatefulMvvmWidget{
  final ElderInfo? info;

  LeaveRecordPage({this.info});

  @override
  State<StatefulWidget> createState() =>_LeaveRecordState();

}

class _LeaveRecordState extends BaseMvvmListState<LeaveRecordViewModel,LeaveRecordPage>{
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            finish();
          },
          icon: Icon(
              Platform.isAndroid ? Icons.arrow_back : Icons.arrow_back_ios),
        ),
        title: Text("请假登记"),
        actions: [
          IconButton(
              onPressed: () {
                key.currentState?.openEndDrawer();
              },
              icon: Icon(Icons.search))
        ],
      ),
      endDrawer: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AppBar(
            leading: null,
            automaticallyImplyLeading: false,
            title: Text("筛选"),
            actions: [Container()],
          ),
          widget.info!=null?Container():CommonText("长者姓名")
              .addToContainer(margin: EdgeInsets.only(left: 16, top: 10)),
          widget.info!=null?Container():CommonInput(
            hintText: "输入长者姓名",
            border: InputBorder.none,
            padding: EdgeInsets.zero,
            isDense: true,
            textInputAction: TextInputAction.search,
            onChanged: (str) {
              viewModel.customerNameLike = str;
            },
            onSubmitted: (str) {
              viewModel.requestRefresh();
            },
          ).addToContainer(
              decoration: BoxDecoration(
                  color: Colors.grey[200],
                  borderRadius: BorderRadius.circular(8)),
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
              padding: EdgeInsets.all(8)),

          Gaps.line,
          IconTitleTextWidget(
            "在院状态",
            text: viewModel.validSignName,
            hintText: "选择在院状态",
          ).onTap(() {
            viewModel.selectStatus();
          }),

          Gaps.midLine,
          CommonText("离院时间")
              .addToContainer(margin: EdgeInsets.only(left: 16, top: 10)),

          IconTitleTextWidget(
            "开始时间",
            text: viewModel.startDateStr,
            hintText: "选择离院开始时间",
          ).onTap(() {
            viewModel.selectStartDate(
                max: DateTime.now(),
                click: (date) {
                  viewModel.requestRefresh();
                });
          }),
          IconTitleTextWidget(
            "结束时间",
            text: viewModel.endDateStr,
            hintText: "选择离院结束时间",
          ).onTap(() {
            viewModel.selectEndDate(
                max: DateTime.now(),
                click: (date) {
                  viewModel.requestRefresh();
                });
          }),
          Gaps.midLine,
          CommonText("回院时间")
              .addToContainer(margin: EdgeInsets.only(left: 16, top: 10)),

          IconTitleTextWidget(
            "开始时间",
            text: viewModel.backTimeStart,
            hintText: "选择回院开始时间",
          ).onTap(() {
            viewModel.selectBackStartDate(
                max: DateTime.now(),
                click: (date) {
                  viewModel.requestRefresh();
                });
          }),
          IconTitleTextWidget(
            "结束时间",
            text: viewModel.backTimeEnd,
            hintText: "选择回院结束时间",
          ).onTap(() {
            viewModel.selectBackEndDate(
                max: DateTime.now(),
                click: (date) {
                  viewModel.requestRefresh();
                });
          }),
          Container().setWeight(1),
          Gaps.line,
          Row(
            children: [
              TextButton(
                onPressed: () {finish();
                },
                child: CommonText("关闭"),
                style: ButtonStyle(
                    minimumSize:
                    WidgetStateProperty.all(Size.fromHeight(50))),
              ).setWeight(1),
              TextButton(
                onPressed: () {finish();
                viewModel.requestRefresh();},
                child: CommonText("确定", textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor:
                    WidgetStateProperty.all(Colours.app_main),
                    minimumSize:
                    WidgetStateProperty.all(Size.fromHeight(50))),
              ).setWeight(1)
            ],
          )
        ],
      ).addToContainer(
        color: Colors.white,
        width: 300,
      ),
      body: Stack(
        children: [
          loadingContentWidget.setLocation(
              left: 0, right: 0, top: 0, bottom: 66),
          ElevatedButton(
            onPressed: () {
              viewModel.toAddSign();
            },
            child: CommonText("新增", textColor: Colors.white, textSize: 17),
            style: ButtonStyle(
              shape: WidgetStateProperty.all(StadiumBorder()),
                backgroundColor: WidgetStateProperty.all(Color(0xfff6ab00)),
                minimumSize: WidgetStateProperty.all(Size.fromHeight(45))),
          ).setLocation(left: 16, right: 16, bottom: 16)
        ],
      ),
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    // var date = DateTime.parse(item.leaveTime??"");
    // var dateFormat = [yyyy,"年",mm,"月",dd,"日"," ",D];
    var timeFormat = [yyyy,"-",mm,"-",dd,' ',HH,":",nn];
    return Stack(
      children: [
        Column(
          children: [
            // CommonText("${formatDate(date, dateFormat, LocaleType.zh)}",
            //     textSize: 12, textColor: Color(0xff666666))
            //     .addToContainer(margin: EdgeInsets.symmetric(vertical: 15)),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [

                // CommonText("${formatDate(date, timeFormat, LocaleType.zh)}", textColor: Color(0xff999999))
                //     .addToContainer(margin: EdgeInsets.only(top: 3)),
                // Container(
                //   width: 18,
                // ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(height: 15,),
                    Row(
                      children: [
                        CommonText("${item.customerElderName}", fontWeight: FontWeight.bold),
                        Container(
                          width: 12,
                        ),
                        CommonText("${item.buildingNo}-${item.roomNumber}", textSize: 14)
                      ],
                    ),
                    Container(
                      height: 8,
                    ),
                    CommonText("请假登记时间：${item.createdDate?.changeTime(format: timeFormat)}", textSize: 14),
                    Container(
                      height: 8,
                    ),
                    CommonText("记录人：${item.leaveRegistrantName}", textSize: 14),
                    Container(
                      height: 8,
                    ),
                    CommonText("外出事由：${item.leaveReasonName}", textSize: 14),
                    Container(
                      height: 8,
                    ),
                    CommonText("联系电话：${item.accompanyMobile}", textSize: 14),
                    Container(
                      height: 8,
                    ),
                    CommonText("陪同人：${item.accompanyName}", textSize: 14),

                    Container(
                      height: 8,
                    ),
                    CommonText("离院时间：${item.leaveTime?.changeTime(format: timeFormat)??""}", textSize: 14),
                    Container(
                      height: 8,
                    ),
                    CommonText("预计销假时间：${item.planBackTime??""}", textSize: 14),
                    Container(
                      height: 8,
                    ),
                    CommonText("实际销假时间：${item.backTime??""}", textSize: 14),
                    Container(
                      height: 15,
                    ),
                  ],
                )
              ],
            ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16))
          ],
        ),
        if("已离院"==item.validSignName)
          CommonText('销假',textSize: 14,textColor: Colors.white).addToContainer(
            decoration: BoxDecoration(
              color: Colours.app_main,
              borderRadius: 5.borderRadius
            ),
            padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10)
          ).onTap(() {
            Go().push(FinishLeavePage(item:item));
          }).setLocation(right: 16,top: 10)
      ],
    ).addToContainer(color: Colors.white);
  }

  @override
  LeaveRecordViewModel createViewModel() {
   return LeaveRecordViewModel(context,info: widget.info);
  }

  @override
  Widget get separatorDivider => Divider(height: 1,color: Colors.grey,);

}